﻿package com.vinyl.components
{
	import fl.core.UIComponent;
	import flash.events.*;
	import flash.events.Event;
	import flash.display.MovieClip;
	import flash.utils.getDefinitionByName;
	
	import com.vinyl.controls.Button;
	import com.vinyl.controls.ButtonView;
	import com.vinyl.controls.SimpleCheckBoxView;
	
	import com.vinyl.controls.RadioGroupManager;
	import com.vinyl.controls.RadioGroup;
	import com.vinyl.controls.IRadioGroupInfo;

    import flash.display.Loader; 
    import flash.display.Sprite; 
    import flash.net.URLRequest; 
    import flash.system.ApplicationDomain; 
    import flash.system.LoaderContext; 
	
	import flash.text.TextField;
	
	[IconFile ("../../../../../comoponents/01_simpleCheckBox/assets/simpleCheckBox.png")]
	public class SimpleCheckBox extends UIComponent 
	{ 
		public static var EVENT_CHANGED: String = 'button_changed';
		public var boundingBox:MovieClip;
		public var value;
		private var _deepLink = '//';
		
		private var sbv:SimpleCheckBoxView;
		private var btn:Button;
		private var ldr:Loader;
		
		[Inspectable(defaultValue="//", type=String)] 
		public function set deepLink( str:String ):void{
			_deepLink= str; 
		} 
		public function get deepLink():String{
			return _deepLink; 
		}
		
		override protected function configUI():void
		{
			super.configUI();
		}
		
		public function set groupName(name:String):void { 
			btn.groupName = name;
			crateRadioGroup();
		}
		
		public function get groupName():String { 
			return btn.groupName; 
		}
		
		public function setSkin(bBiew: ButtonView ):void
		{
			removeChild( sbv );
			btn.removeEventListener( Button.EVENT_CHANGED, onButtonActionHandler );
			
			btn = new Button( bBiew, false );
			btn.addEventListener( Button.EVENT_CHANGED, onButtonActionHandler );
			addChild( bBiew );
		}
		
		public function setValue( value ):void
		{
			btn.setValue( value );
		}
		
		public function SimpleCheckBox() 
		{ 
			super();
			sbv = new SimpleCheckBoxView();			
			btn = new Button( sbv, false );
			btn.addEventListener( Button.EVENT_CHANGED, onButtonActionHandler );
			
			addChild( sbv );
			init();
		}
		
		private function crateRadioGroup()
		{
			var rgi = RadioGroupManager.getInstance().getRadioGroupInfo( btn.groupName );
			rgi.addRadioButton( btn )
		}
		
		private function init() 
		{ 
			boundingBox.width = 0;
			boundingBox.height = 0;
			boundingBox.visible = false;
		}
		
		private function onButtonActionHandler( evt ): void
		{
			value = evt.target.getValue();
			dispatchEvent( new Event( EVENT_CHANGED ) );
		}
	}
}
